package Eyjafjalla.monster;

import Eyjafjalla.action.LongWaitAction;
import Eyjafjalla.action.ReduceFlightAction;
import Eyjafjalla.power.GiveRedCloudPower;
import Eyjafjalla.power.WhiteCloudPower;
import com.badlogic.gdx.Gdx;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.common.*;
import com.megacrit.cardcrawl.actions.utility.WaitAction;
import com.megacrit.cardcrawl.cards.DamageInfo;
import com.megacrit.cardcrawl.core.AbstractCreature;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.MonsterStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.powers.AbstractPower;
import com.megacrit.cardcrawl.powers.FlightPower;
import com.megacrit.cardcrawl.powers.StrengthPower;

public class MTFKY extends AbstractEyjafjallaMonster{
    public static final String ID = "eyjafjalla:MTFKY";
    private static final String MID_PATH = "MTFKY/enemy_1347_fyshp_2";
    private static final MonsterStrings monsterStrings;
    private static final EnemyType TYPE = EnemyType.NORMAL;

    private static final String IDLE_NAME = "Idle";
    private static final String DIE_NAME = "Die";
    private static final String MOVE_NAME = "Move";

    private static final String A_TO_B = "B_Start";

    private static final String B_ATTACK_NAME = "B_Attack";
    private static final String B_IDLE_NAME = "B_Idle";
    private static final String B_DIE_NAME = "B_Die";
    private static final String B_MOVE_NAME = "B_Move";

    private static final String C_TO_B = "C_Stun";

    private static final String C_ATTACK_NAME = "C_Attack";
    private static final String C_IDLE_NAME = "C_Idle";
    private static final String C_DIE_NAME = "C_Die";

    JMCPS jm = null;

    static {
        monsterStrings = CardCrawlGame.languagePack.getMonsterStrings(ID);
    }

    int attack;
    int attackFly;
    int attackFly2;
    int defend;
    int defendFly;
    int white = 2;
    int fly =4;
    int strength = 4;
    float waitTime = 0.3F;

    int strengthTimes = 0;
    boolean firstTurn;

    int currentStance = 1;
    boolean isMoving = false;
    float moveTimer;
    float startMoveTimer = 2.5F;
    float startX;
    float aimX;

    public MTFKY(float x,float y){
        super(monsterStrings.NAME,ID,TYPE,96,240F,230F,x,y);
        setSpine(MID_PATH,1.6F);
        setFastMode();
        this.state.setAnimation(0,IDLE_NAME,true);
        this.currentStance = 1;
        if(AbstractDungeon.ascensionLevel>=8){
            setHp(150);
        }
        else {
            setHp(126);
        }
        if(AbstractDungeon.ascensionLevel>=3){
            attack = 48;
            attackFly = 21;
            attackFly2 = 42;
        }
        else {
            attack = 36;
            attackFly = 15;
            attackFly2 = 30;
        }
        if(AbstractDungeon.ascensionLevel>=18){
            defend = 18;
            defendFly = 18;
            white = 3;
            fly = 6;
        }
        else {
            defend = 12;
            defendFly = 12;
            white = 2;
            fly = 5;
        }

        firstTurn = true;

        this.damage.add(new DamageInfo(this,this.attackFly, DamageInfo.DamageType.NORMAL));
        this.damage.add(new DamageInfo(this,this.attackFly2, DamageInfo.DamageType.NORMAL));
        this.damage.add(new DamageInfo(this,this.attack, DamageInfo.DamageType.NORMAL));
    }

    public void setFastMode(){
            state.setTimeScale(1);
            this.waitTime = 0.3F;
    }

    @Override
    public void usePreBattleAction() {
        this.addToBot(new ApplyPowerAction(this,this,new FlightPower(this,fly),fly));
        super.usePreBattleAction();
    }

    @Override
    protected void getMove(int i) {
        if(this.currentStance==1){
            this.setMove((byte) 1,Intent.UNKNOWN);
        }
        else if(this.currentStance==2){
            if(!firstTurn&&strengthTimes<(AbstractDungeon.ascensionLevel>=18?3:2)&&!lastMove((byte)6)){
                this.setMove((byte) 6,Intent.BUFF);
                return;
            }
            if((i<60&&!lastTwoMoves((byte) 2))||lastMove((byte) 3)){
                this.setMove((byte) 2,Intent.ATTACK_DEBUFF,this.damage.get(0).base);
            }
            else {
                this.setMove((byte) 3,Intent.ATTACK,this.damage.get(1).base);
            }
        }
        else if(this.currentStance==3){
            boolean hasCPS = false;
            for(AbstractMonster m:AbstractDungeon.getMonsters().monsters){
                if(!m.isDeadOrEscaped()&&m instanceof JMCPS){
                    hasCPS = true;
                    break;
                }
            }
            if(hasCPS){
                this.setMove((byte) 4,Intent.DEFEND_BUFF);
            }
            else {
                this.setMove((byte) 5,Intent.ATTACK,damage.get(2).base);
            }
        }
    }

    private void movePosition(float x, float y){
        drawX = x;
        drawY = y;
        animX = 0.0F;
        animY = 0.0F;
        hb.move(drawX+hb_x,drawY+hb_y+hb_h/2.0F);
        healthHb.move(hb.cX, hb.cY - hb_h / 2.0F - healthHb.height / 2.0F);
        refreshIntentHbLocation();
    }

    @Override
    public void update() {
        if(isMoving){
            if(moveTimer>0F){
                moveTimer -= Gdx.graphics.getDeltaTime();
                this.movePosition(startX - (startX-aimX)*(startMoveTimer-moveTimer)/startMoveTimer,drawY);
            }
            else {
                isMoving = false;
                if(currentStance==1){
                    this.state.setAnimation(0,IDLE_NAME,true);
                    addToTop(new ChangeStateAction(this,"TAKE_UP"));
                }
                else if(currentStance==3){
                    this.flipHorizontal = true;
                    this.skeleton.setFlipX(true);
                    this.state.setAnimation(0,B_IDLE_NAME,true);
                }
            }
        }
        super.update();
    }

    @Override
    public void takeTurn() {
        setFastMode();
        if(currentStance==2){
            firstTurn = false;
        }
        switch (this.nextMove){
            case 1:{
                boolean takeUp = false;

                for(AbstractMonster m:AbstractDungeon.getMonsters().monsters){
                    if(!m.isDeadOrEscaped()&&m instanceof JMCPS){
                        takeUp = true;
                        jm = (JMCPS) m;

                        break;
                    }
                }
                if(takeUp){
                    addToBot(new ChangeStateAction(this,"MOVE"));
                    addToBot(new LongWaitAction(2.6F));
                }
                else {
                    this.addToBot(new RemoveSpecificPowerAction(this,this,FlightPower.POWER_ID));
                }
                break;
            }
            case 2:{
                if(this.currentStance==2)
                    addToBot(new ChangeStateAction(this,"C_ATTACK"));
                else if(this.currentStance==3){
                    addToBot(new ChangeStateAction(this,"B_ATTACK"));
                }
                addToBot(new WaitAction(this.waitTime));
                addToBot(new DamageAction(AbstractDungeon.player,this.damage.get(0), AbstractGameAction.AttackEffect.SLASH_DIAGONAL));
                addToBot(new ApplyPowerAction(AbstractDungeon.player,AbstractDungeon.player,new WhiteCloudPower(AbstractDungeon.player,white),white));
                break;
            }
            case 3:{
                if(this.currentStance==2)
                    addToBot(new ChangeStateAction(this,"C_ATTACK"));
                else if(this.currentStance==3){
                    addToBot(new ChangeStateAction(this,"B_ATTACK"));
                }
                addToBot(new WaitAction(this.waitTime));
                addToBot(new DamageAction(AbstractDungeon.player,this.damage.get(1), AbstractGameAction.AttackEffect.SLASH_HEAVY));
                break;
            }
            case 4:{
                for(AbstractMonster m:AbstractDungeon.getMonsters().monsters){
                    if(m.isDeadOrEscaped())
                        continue;
                    addToBot(new GainBlockAction(m,this,this.defend));
                    addToBot(new ApplyPowerAction(m,this,new GiveRedCloudPower(m,1),1));
                }
                break;
            }
            case 5:{
                addToBot(new ChangeStateAction(this,"B_ATTACK"));
                addToBot(new WaitAction(this.waitTime));
                addToBot(new DamageAction(AbstractDungeon.player,this.damage.get(2), AbstractGameAction.AttackEffect.SLASH_HEAVY));
                break;
            }
            case 6:{
                strengthTimes++;
                addToBot(new ApplyPowerAction(this,this,new GiveRedCloudPower(this,1),1));
                addToBot(new ApplyPowerAction(this,this,new StrengthPower(this,strength),strength));
                addToBot(new ReduceFlightAction(this));
                break;
            }
        }
        addToBot(new RollMoveAction(this));
    }

    @Override
    public void changeState(String stateName) {
        switch (stateName){
            case "C_ATTACK":{
                this.state.setAnimation(0,C_ATTACK_NAME,false);
                this.state.addAnimation(0,C_IDLE_NAME,true,0F);
                break;
            }
            case "MOVE": {
                if (jm != null) {
                    this.isMoving = true;
                    this.moveTimer = startMoveTimer;
                    this.startX = drawX;
                    this.aimX = jm.drawX;
                    this.state.setAnimation(0, MOVE_NAME, true);
                }
                break;
            }
            case "MOVE_2":{
                this.isMoving = true;
                this.flipHorizontal = false;
                this.skeleton.setFlipX(false);
                this.moveTimer = startMoveTimer;
                this.startX = drawX;
                this.aimX = drawX + 240F *Settings.scale;
                this.state.setAnimation(0, B_MOVE_NAME, true);
                break;
            }
            case "TAKE_UP":{
                if(jm!=null){
                    this.currentStance = 2;
                    this.currentHealth += jm.currentHealth;
                    this.maxHealth += jm.maxHealth;
                    this.healthBarUpdatedEvent();

                    jm.dontRender = true;
                    jm.currentHealth = 0;
                    jm.healthBarUpdatedEvent();
                    jm.damage(new DamageInfo((AbstractCreature)null, 0, DamageInfo.DamageType.HP_LOSS));
                    CardCrawlGame.sound.play("POWER_FLIGHT", 0.05F);
                    this.state.setAnimation(0,C_IDLE_NAME,true);
                }
                else {
                    addToTop(new RemoveSpecificPowerAction(this,this,FlightPower.POWER_ID));
                }
                break;
            }
            case "GROUNDED":{
                if(currentStance==2){
                    this.state.setAnimation(0,C_TO_B,false);
                    this.state.addAnimation(0,B_IDLE_NAME,true,0F);
                    this.setMove((byte) 4,Intent.DEFEND_BUFF);
                    this.createIntent();
                    JMCPS njm = new JMCPS(-100F,0F);

                    //均分生命值和最大生命，以及力量

                    this.currentHealth /= 2;
                    if(this.currentHealth<1)
                        this.currentHealth = 1;
                    this.healthBarUpdatedEvent();
                    int strengthAmt = 0;
                    AbstractPower st = this.getPower(StrengthPower.POWER_ID);
                    if(st!=null){
                        strengthAmt = st.amount/2;
                        if(strengthAmt>0){
                            addToBot(new ReducePowerAction(this,this,st,st.amount-strengthAmt));
                        }
                    }
                    njm.strengthTimes = this.strengthTimes;

                    njm.currentHealth = this.currentHealth;
                    njm.maxHealth = this.maxHealth;
                    njm.setMove((byte) 4,Intent.STUN);
                    njm.createIntent();
                    this.addToBot(new LongWaitAction(1.1F));
                    this.addToBot(new SpawnMonsterAction(njm,false,0));
                    this.addToBot(new ChangeStateAction(this,"MOVE_2"));
                    if(strengthAmt>0)
                        addToBot(new ApplyPowerAction(njm,this,new StrengthPower(njm,strengthAmt),strengthAmt));
                }
                else if(currentStance==1){
                    this.setMove((byte) 7,Intent.STUN);
                    this.createIntent();
                    this.state.setAnimation(0,A_TO_B,false);
                    this.state.addAnimation(0,B_IDLE_NAME,true,0F);
                }
                this.currentStance = 3;
                break;
            }
            case "B_ATTACK":{
                this.state.setAnimation(0,B_ATTACK_NAME,false);
                this.state.addAnimation(0,B_IDLE_NAME,true,0F);
                break;
            }
            default:
                break;
        }
    }

    @Override
    public void die() {
        this.state.setTimeScale(1F);
        if(currentStance==1)
            state.setAnimation(0,DIE_NAME,false);
        else if(currentStance==2)
            state.setAnimation(0,C_DIE_NAME,false);
        else if(currentStance==3)
            state.setAnimation(0,B_DIE_NAME,false);

        super.die();

    }
}

